package com.focusai.efairy.utils.report;

import com.focusai.efairy.model.report.AlarmDataEntity;
import com.focusai.efairy.model.report.ChildWarnningDataEntity;
import com.focusai.efairy.model.report.WarnningDataType;
import com.focusai.efairy.utils.DateUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by luo_guangwen on 2017/9/23.
 */

public class ReportUtils {

    //1-日报 2-周报 3-月报 4-季报 5-年报，传区间内的一个时间
    public static final int DAY = 1;
    public static final int WEEK = 2;
    public static final int MONDTH = 3;
    public static final int SESION = 4;
    public static final int YEAR = 5;
    public static final int CUSTOM = 6;

    public static WarnningDataType getWarnningDatas(List<AlarmDataEntity> alarmDataEntities, long startDate, long endDate, int dateType) {
        long sectionTime = ReportUtils.getXSectionByDateType(dateType);
        List<ChildWarnningDataEntity> list = new ArrayList<>();
        int count = ReportUtils.getXCountByDateType(dateType, startDate);
        if (dateType == ReportUtils.CUSTOM){
            sectionTime = (endDate - startDate)/count;
        }
        for (int i = 0; i < count; i++) {
            long start = startDate + i * sectionTime;
            list.add(new ChildWarnningDataEntity(start, start + sectionTime));
        }
        for (AlarmDataEntity entity : alarmDataEntities) {
            Date date = DateUtils.parseDate(entity.efairydevicefiredata_time);
            if (date == null)
                continue;
            long times = date.getTime();
            for (ChildWarnningDataEntity sectionEntity : list) {
                if (times >= sectionEntity.startDataTime && times < sectionEntity.endDataTime) {
                    sectionEntity.value++;
                    break;
                }
            }
        }
        WarnningDataType warnningDataType = new WarnningDataType();
        warnningDataType.setDataEntities(list);
        warnningDataType.setxCount(count);
        warnningDataType.setDateType(dateType);
        return warnningDataType;
    }

    /**
     * 1-日报 2-周报 3-月报 4-季报 5-年报，传区间内的一个时间
     */
    public static long getXSectionByDateType(int type) {
        long count = DateUtils.ONE_HOUR * 2;
        switch (type) {
            case 1:
                break;
            case 2:
            case 3:
                count = DateUtils.ONE_DAY;
                break;
            case 4:
            case 5:
                count = DateUtils.ONE_MONTH;
                break;

            default:
                break;
        }
        return count;
    }

    /**
     * 1-日报 2-周报 3-月报 4-季报 5-年报，传区间内的一个时间
     */
    public static int getXCountByDateType(int type, long startDate) {
        int count = 7;
        switch (type) {
            case 1:
                count = 13;
                break;
            case 2:
                count = 7;
                break;
            case 3:
                count = DateUtils.getcurMonthDayOfYear(startDate);
                break;
            case 4:
                count = 3;
                break;
            case 5:
                count = 12;
                break;
            case 6:
                count = 12;
                break;
            default:
                break;
        }
        return count;
    }
}
